package com.wl.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wl.entity.Delivery_07;
import com.wl.entity.Loginfo;
import com.wl.service.Delivery_07Service;
import com.wl.util.PageData;

/**
 * Delivery_07控制器
 * 
 * @author HCH
 *
 */
@Controller
public class Delivery_07Controller {

	// 注入Delivery_07Service
	@Autowired
	private Delivery_07Service deliveryService;

	/**
	 * 动态sql语句添加业务信息
	 * 
	 * @param record
	 * @return
	 */
	@PostMapping(value = "adddel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean testInsertSelective(@RequestBody Delivery_07 record) {
		// deliverystatus状态默认为1，表示尚未处理
		record.setDeliverystatus(1);
		// 调用insertSelective()方法
		boolean flag = deliveryService.insertSelective(record);
		return flag;
	}

	/**
	 * 根据当前处理人编号查找尚未处理的业务并分页
	 * 
	 * @param page
	 * @param limit
	 * @param deliveryno
	 * @return
	 */
	@GetMapping(value = "getdelbydeliveryno", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Delivery_07> selectDeliveryByDeliveryno(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "deliveryno", defaultValue = "") String deliveryno) {
		PageData<Delivery_07> pageData = deliveryService.selectDeliveryByDeliveryno(page, limit, deliveryno);
		return pageData;
	}

	/**
	 * 根据flowid动态修改业务信息
	 * 
	 * @param record
	 * @return
	 */
	@PutMapping(value = "upddelbyid", produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean updateByPrimaryKeySelective(@RequestBody Delivery_07 delivery) {
		// 获取当前时间
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String datetimenow = df.format(new Date());
		// 将当系统日期和时间赋值给对象
		delivery.setApprovetime(datetimenow);
		// 修改处理状态为0，表示已处理
		delivery.setDeliverystatus(0);
		// 创建Loginfo对象 用于增加日志
		Loginfo log = new Loginfo();
		log.setPerson(delivery.getDeliveryno());
		log.setFlowid(delivery.getFlowid());
		log.setLogcontent("已领料");
		log.setLogdate(datetimenow);
		// 调用updateByPrimaryKeySelective()方法
		boolean flag = deliveryService.updateByPrimaryKeySelective(delivery, log);
		return flag;
	}

	/**
	 * 查询已审批数量，用于页面展示数据
	 * 
	 * @return
	 */
	@GetMapping(value = "getCountBydelivery")
	@ResponseBody
	public int getCountBydelivery() {
		return deliveryService.getCountByDelivery();
	}
}
